package mi.payment.ui.query;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import java.util.List;
import java.util.Locale;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MoneyUtils;
import sunmi.sunmiui.title.Title;


/**
 * 交易汇总页面
 */
public class TransactionSummaryActivity extends ParentActivity {

    private View WechatConsumption;
    private View wechatReturnGoods;
    private View wechatTotal;
    private View alipayConsumption;
    private View alipayReturnGoods;
    private View alipayTotal;
    private View unionConsumption;
    private View unionReturnGoods;
    private View unionTotal;
    private View bankCardConsumption;
    private View bankCardReturnGoods;
    private View bankCardTotal;

    private View unionLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_sunmmary);
        initView();
        initData();
    }

    private void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorBar));
        }
        Title title = (Title) findViewById(R.id.title);
        title.setBackgroundResource(R.color.colorBar);
        title.setButtonVisibility(false, true, true, true);
        title.setTitleText(getString(R.string.trans_summary));
        title.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, null, null, null);

        unionLayout = findViewById(R.id.union_layout);

        WechatConsumption = findViewById(R.id.wechat_consumption);
        wechatReturnGoods = findViewById(R.id.wechat_return_goods);
        wechatTotal = findViewById(R.id.wechat_total);
        alipayConsumption = findViewById(R.id.alipay_consumption);
        alipayReturnGoods = findViewById(R.id.alipay_return_goods);
        alipayTotal = findViewById(R.id.alipay_total);

        unionConsumption = findViewById(R.id.union_consumption);
        unionReturnGoods = findViewById(R.id.union_return_goods);
        unionTotal = findViewById(R.id.union_total);

        bankCardConsumption = findViewById(R.id.bank_card_consumption);
        bankCardReturnGoods = findViewById(R.id.bank_card_return_goods);
        bankCardTotal = findViewById(R.id.bank_card_total);

        setViewTextSizeAndColor(wechatTotal, 15);
        setViewTextSizeAndColor(alipayTotal, 15);
        setViewTextSizeAndColor(unionTotal, 15);
        setViewTextSizeAndColor(bankCardTotal, 15);

        String strConsumption = getString(R.string.consume_im_title);//消费
        String strRefund = getString(R.string.refund);//退款
        String strTotal = getString(R.string.summny);//总计

        //微信
        setViewText(WechatConsumption, getString(R.string.wechat) + strConsumption, 0, 0);
        setViewText(wechatReturnGoods, getString(R.string.wechat) + strRefund, 0, 0);
        setViewText(wechatTotal, getString(R.string.wechat) + strTotal, 0, 0);
        //支付宝
        setViewText(alipayConsumption, getString(R.string.alipay) + strConsumption, 0, 0);
        setViewText(alipayReturnGoods, getString(R.string.alipay) + strRefund, 0, 0);
        setViewText(alipayTotal, getString(R.string.alipay) + strTotal, 0, 0);
        //银联二维码
        setViewText(unionConsumption, getString(R.string.union1) + getString(R.string.platform_code) + strConsumption, 0, 0);
        setViewText(unionReturnGoods, getString(R.string.union1) + getString(R.string.platform_code) + strRefund, 0, 0);
        setViewText(unionTotal, getString(R.string.union1) + getString(R.string.platform_code) + strTotal, 0, 0);
        //银行卡
        setViewText(bankCardConsumption, getString(R.string.platform_bank_card) + getString(R.string.collect_money), 0, 0);
        setViewText(bankCardReturnGoods, getString(R.string.platform_bank_card) + strRefund, 0, 0);
        setViewText(bankCardTotal, getString(R.string.platform_bank_card) + strTotal, 0, 0);
    }

    /**
     * 初始化文本，设置类型、笔数、总金额
     */
    private void setViewText(View layout, String type, int num, long totalAmount) {
        ((TextView) layout.findViewById(R.id.type)).setText(type);
        ((TextView) layout.findViewById(R.id.num)).setText(String.valueOf(num));
        ((TextView) layout.findViewById(R.id.total_amount)).setText(String.valueOf(totalAmount));
    }

    /**
     * 设置总计行文字大小
     */
    private void setViewTextSizeAndColor(View layout, int size) {
        int[] ids = {R.id.type, R.id.num, R.id.total_amount,};
        for (int i = 0; i < ids.length; i++) {
            TextView tv = (TextView) layout.findViewById(ids[i]);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
            tv.setTextColor(Color.parseColor("#FD5A52"));
        }
    }

    /**
     * 更新笔数、总金额
     */
    private void updateViewText(View layout, int count, long totalAmount, boolean forceMinus) {
        ((TextView) layout.findViewById(R.id.num)).setText(String.valueOf(count));
        String totalAmountStr = getTotalAmountStr(totalAmount);
        if (forceMinus && !totalAmountStr.startsWith("-")) {//强制显示负号
            totalAmountStr = "-" + totalAmountStr;
        }
        ((TextView) layout.findViewById(R.id.total_amount)).setText(totalAmountStr);
    }

    /**
     * 总金额字符串
     */
    private String getTotalAmountStr(long totalAmount) {
        String format = getString(R.string.rmb) + "%s";//format 值为-￥%s或￥%s
        return String.format(Locale.getDefault(), format, MoneyUtils.longCent2DoubleMoneyStr(Math.abs(totalAmount)));
    }

    private void initData() {
        executeTask(new Runnable() {

            /** 计算总金额 */
            private long getTotalAmount(List<PayDetail> payDetails) {
                long totalAmount = 0;
                for (PayDetail payDetail : payDetails) {
                    totalAmount += payDetail.getAmount();
                }
                return totalAmount;
            }

            @Override
            public void run() {
                // 微信
                final List<PayDetail> wechatConsumptionList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(2, 1);//微信消费
                final List<PayDetail> wechatReturnGoodsList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(2, 2);//微信撤销
                wechatReturnGoodsList.addAll(mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(2, 3));//微信退货
                // 支付宝
                final List<PayDetail> alipayConsumptionList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(1, 1);//支付宝消费
                final List<PayDetail> alipayReturnGoodsList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(1, 2);//支付宝撤销
                alipayReturnGoodsList.addAll(mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(1, 3));//支付宝退货
                // 银联二维码
                final List<PayDetail> unionConsumptionList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(4, 1);//银联二维码消费
                final List<PayDetail> unionReturnGoodsList = mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(4, 2);//银联二维码撤销
                unionReturnGoodsList.addAll(mPayDetailModel.getPayDetailByQrCodeTransPlatformAndQrCodeTransType(4, 3));//银联二维码退货

                // 银行卡
                final List<PayDetail> bankCardConsumptionList = mPayDetailModel.getSuccessPayDetailByTransactionType(1);//银行卡消费
                bankCardConsumptionList.addAll(mPayDetailModel.getSuccessPayDetailByTransactionType(6));//银行卡预授权完成

                final List<PayDetail> bankCardReturnGoodsList = mPayDetailModel.getSuccessPayDetailByTransactionType(2);//银行卡撤销
                bankCardReturnGoodsList.addAll(mPayDetailModel.getSuccessPayDetailByTransactionType(3));//银行卡退货
                bankCardReturnGoodsList.addAll(mPayDetailModel.getSuccessPayDetailByTransactionType(7));//银行卡预授权完成撤销

                runOnUI(new Runnable() {
                    @Override
                    public void run() {
                        // 如果没银联二维码的记录隐藏布局
                        if (unionConsumptionList.size() < 1) {
                            unionLayout.setVisibility(View.GONE);
                        }

                        //微信
                        long consumptionTotalAmount = getTotalAmount(wechatConsumptionList);//消费总金额
                        long returnGoodsTotalAmount = getTotalAmount(wechatReturnGoodsList);//退货总金额
                        long totalAmount = consumptionTotalAmount - returnGoodsTotalAmount;//总金额
                        updateViewText(WechatConsumption, wechatConsumptionList.size(), consumptionTotalAmount, false);
                        updateViewText(wechatReturnGoods, wechatReturnGoodsList.size(), returnGoodsTotalAmount, true);
                        updateViewText(wechatTotal, wechatConsumptionList.size() + wechatReturnGoodsList.size(),
                                Math.abs(totalAmount), totalAmount < 0);
                        //支付宝
                        consumptionTotalAmount = getTotalAmount(alipayConsumptionList);
                        returnGoodsTotalAmount = getTotalAmount(alipayReturnGoodsList);
                        totalAmount = consumptionTotalAmount - returnGoodsTotalAmount;
                        updateViewText(alipayConsumption, alipayConsumptionList.size(), consumptionTotalAmount, false);
                        updateViewText(alipayReturnGoods, alipayReturnGoodsList.size(), returnGoodsTotalAmount, true);
                        updateViewText(alipayTotal, alipayConsumptionList.size() + alipayReturnGoodsList.size(),
                                Math.abs(totalAmount), totalAmount < 0);

                        //银联二维码
                        consumptionTotalAmount = getTotalAmount(unionConsumptionList);
                        returnGoodsTotalAmount = getTotalAmount(unionReturnGoodsList);
                        totalAmount = consumptionTotalAmount - returnGoodsTotalAmount;
                        updateViewText(unionConsumption, unionConsumptionList.size(), consumptionTotalAmount, false);
                        updateViewText(unionReturnGoods, unionReturnGoodsList.size(), returnGoodsTotalAmount, true);
                        updateViewText(unionTotal, unionConsumptionList.size() + unionReturnGoodsList.size(),
                                Math.abs(totalAmount), totalAmount < 0);

                        //银行卡
                        consumptionTotalAmount = getTotalAmount(bankCardConsumptionList);
                        returnGoodsTotalAmount = getTotalAmount(bankCardReturnGoodsList);
                        totalAmount = consumptionTotalAmount - returnGoodsTotalAmount;
                        updateViewText(bankCardConsumption, bankCardConsumptionList.size(), consumptionTotalAmount, false);
                        updateViewText(bankCardReturnGoods, bankCardReturnGoodsList.size(), returnGoodsTotalAmount, true);
                        updateViewText(bankCardTotal, bankCardConsumptionList.size() + bankCardReturnGoodsList.size(),
                                Math.abs(totalAmount), totalAmount < 0);
                    }
                });
            }
        });
    }

}
